<template>
  <div ref="chart" :style="{ width: width, height: height }"></div>
</template>

<script>
import echarts from "echarts";

export default {
  name: "ChartBar",
  props: {
    data: {
      // 数据
      type: Array,
      required: true,
    },
    xAxisData: {
      // x轴
      type: Array,
      required: true,
    },
    legendData: {
      type: Array,
      required: true,
    },
    width: {
      // 宽高
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
  },
  mounted() {
    this.renderChart();
  },
  methods: {
    renderChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);
      const { data, xAxisData, legendData } = this; // 根据具体数据格式再处理

      const option = {
        legend: {
          show: true,
          textStyle: {
            fontSize: 12,
            color: "red",
            lineHeight: 12,
            rich: {
              a: {
                verticalAlign: "middle",
              },
            },
            // padding: [3, 0, 0, 0],
            data: legendData,
          },
        },
        grid: {
          left: 40,
          right: 20,
          top: 50,
          bottom: 25,
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          right: 0,
          data: legendData,
        },
        xAxis: {
          type: "category",
          // axisTick: { show: true },
          data: xAxisData,
          axisLine: {
            lineStyle: {
              color: "#6666", // x轴线条颜色
              type: "solid",
            },
          },
          axisTick: {
            show: false, // 隐藏刻度线
          },
          axisLabel: {
            interval: 0, // 强制显示所有标签
            // rotate: 45, // 旋转角度
            margin: 10, // 与轴线的距离
            textStyle: {
              fontSize: "12px",
              color: "#666666", // 文字颜色
            },
          },
        },

        yAxis: [
          {
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            type: "value",
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
          },
        ],
        series: data.map((item, index) => ({
          name: legendData[index],
          type: "bar",
          barWidth: 17, // 设置柱状图宽度
          itemStyle: {
            // 设置柱状图颜色
            color: index === 0 ? "#00998B" : "#FFAF25",
          },
          data: item,
        })),
      };

      myChart.setOption(option);
    },
  },
  watch: {
    data: {
      handler() {
        this.renderChart();
      },
      deep: true,
    },
    xAxisData: {
      handler() {
        this.renderChart();
      },
      deep: true,
    },
    legendData: {
      handler() {
        this.renderChart();
      },
      deep: true,
    },
  },
};
</script>

<style scoped>
</style>
